/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.model;

import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.beans.IntrospectionException;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import javax.swing.AbstractAction;
import javax.swing.Action;
import org.openide.cookies.SaveCookie;
import org.openide.nodes.Children;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;
import org.openide.util.WeakListeners;

/**
 *
 * @author Timon Veenstra
 */
public abstract class BeanNode<B extends Bean> extends org.openide.nodes.BeanNode<B> implements PropertyChangeListener {

    private final DataProvider dataProvider;
    private final BeanSaveCookie saveCookie;

    public BeanNode(B bean, Children children, Lookup lkp, DataProvider dataProvider) throws IntrospectionException {
        super(bean, children, lkp);
        this.dataProvider = dataProvider;
        assert dataProvider != null;
        saveCookie = new BeanSaveCookie(bean);
        bean.addPropertyChangeListener(WeakListeners.propertyChange(this, bean));
        bean.addUndoableEditListener(dataProvider.getUndoredoManager());
    }

    public BeanNode(B bean, Children children, DataProvider dataProvider) throws IntrospectionException {
        this(bean, children,null,dataProvider);
    }

    public BeanNode(B bean, DataProvider dataProvider) throws IntrospectionException {
        this(bean, null,null,dataProvider);
    }


    /**
     * Add the savecookie to those entities that are persistable
     *
     * @param modified Only add cookie if the property that fired this method has really been modified.
     */
    protected void fire(boolean modified) {
        if (modified) {
            dataProvider.addSaveCookie(saveCookie);
        } else {
            dataProvider.removeSaveCookie(saveCookie);
        }
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if ("name".equals(evt.getPropertyName())) {
            this.fireDisplayNameChange(null, getDisplayName());
        }
        fire(true);
    }

    @Override
    public Action[] getActions(boolean context) {
        return new Action[]{new OpenPropertiesAction()};
    }

    @Override
    public B getBean() {
        return super.getBean();
    }

    public class OpenPropertiesAction extends AbstractAction {

        public OpenPropertiesAction() {
            super(NbBundle.getMessage(OpenPropertiesAction.class, "bean_node_action.openproperties"));
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            PropertyViewerTopComponent tc = PropertyViewerTopComponent.findInstance();
            if (null != tc) {
                tc.open();
                tc.requestActive();
            }
        }
    }

    private class BeanSaveCookie implements SaveCookie {

        private final Bean bean;

        public BeanSaveCookie(Bean bean) {
            this.bean = bean;
        }

        @Override
        public void save() throws IOException {
            dataProvider.save(bean);
            fire(false);
        }
    }
    
    @Override
    public Transferable drag() throws IOException {
        return getBean();
    }      
}
